import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import EmployeeView from '@/views/personnel/EmployeeView.vue'
import UserView from '@/views/personnel/userView.vue'
import DepartmentView from '@/views/department/departmentView.vue'
import PartView from '@/views/part/PartView.vue'
import OrderView from '@/views/order/OrderView.vue'
import TaskView from '@/views/order/TaskView.vue'
import ProcessingView from '@/views/order/ProcessingView.vue'
import MyOrderView from '@/views/order/MyOrderView.vue'
import SubmitOrderView from '@/views/order/SubmitOrderView.vue'
import IndexView from '@/views/IndexView.vue'
import UserInfoView from '@/views/user/UserInfoView.vue'
import AvatarView from '@/views/user/AvatarView.vue'
import PassWordView from '@/views/user/PassWordView.vue'
import RegisterView from '@/views/RegisterView.vue'
import { tokenStore } from '@/stores/token'
import { ElMessage } from 'element-plus'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      redirect:'/index',
      children:[
        {path:'/emp',name:'employee',component:EmployeeView},
        {path:'/index',name:'index',component:IndexView},
        {path:'/user',name:'user',component:UserView},
        {path:'/depat',name:'department',component:DepartmentView},
        {path:'/part',name:'part',component:PartView},
        {path:'/appoint',name:'appoint',component:OrderView},
        {path:'/task',name:'task',component:TaskView},
        {path:'/processing',name:'processing',component:ProcessingView},
        {path:'/myOrder',name:'myOrder',component:MyOrderView},
        {path:'/submitOrder',name:'submitOrder',component:SubmitOrderView},
        {path:'/userInfo',name:'userInfo',component:UserInfoView},
        {path:'/avatar',name:'avatar',component:AvatarView},
        {path:'/password',name:'password',component:PassWordView}
      ]
    },
    {
      path: '/login',
      name: 'login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/LoginView.vue')
    },
    {
      path:'/register',
      name:'register',
      component:RegisterView
    }
  ]
})

router.beforeEach((to, from) => {
  if (to.path == '/login' || to.path === '/register') {
    return true;
  } else {
    let userToken = tokenStore()
    // if (!sessionStorage.getItem('token')) {
    if (!userToken.getToken) {
      ElMessage.error("请求错误")
      return "/login";
    } else {
      return true;
    }
  }
})

export default router
